//
//  WenZhangDViewController.m
//  JiuJiuShangCheng
//
//  Created by jiujiukeji on 2019/2/20.
//  Copyright © 2019年 jiujiukeji. All rights reserved.
//

#import "WenZhangDViewController.h"
#import <WebKit/WebKit.h>
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/ShareSDK+SSUI.h>
#import <ShareSDKUI/ShareSDKUI.h>
#import <ShareSDK/ShareSDK+Base.h>

@interface WenZhangDViewController ()<UIWebViewDelegate>

@end

@implementation WenZhangDViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = UIColorWhite;
    [self loadWhiteTitleLable:@"文章"];
    self.rightBtn.hidden = NO;
    [self.rightBtn setTitle:@"分享" forState:(UIControlStateNormal)];
    [self.rightBtn setTitleColor:UIColorWithRGB(36, 36, 36) forState:UIControlStateNormal];
    [self.rightBtn addTarget:self action:@selector(shareActionP) forControlEvents:(UIControlEventTouchUpInside)];
    
    // 1.创建webview
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, kScreenWidth, kScreenHeight-SafeAreaTopHeight)];
    webView.delegate = self;
    
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/xcx.aspx?skinid=news&companyid=%@&id=%@",kServerURL,companyId,self.Aid]]]];
    // 最后将webView添加到界面
    [self.view addSubview:webView];
    
}
- (void)dealloc
{
    //    NSLog(@"控制器被销毁了");
    [self clearCache]; // 清除缓存
}
#pragma mark - 分享按钮
- (void)shareActionP
{
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    [shareParams SSDKSetupShareParamsByText:@"点击查看详情"
                                     images:@[[NSString stringWithFormat:@"%@",self.imgUrl]]
                                        url:[NSURL URLWithString:[NSString stringWithFormat:@"%@/content/%@.aspx",shareUrl,self.Aid]]
                                      title:self.titleStr
                                       type:SSDKContentTypeAuto];
    
    //有的平台要客户端分享需要加此方法，例如微博
    [shareParams SSDKEnableUseClientShare];
    //    [shareParams SSDKEnableExtensionShare];
    //2、分享（可以弹出我们的分享菜单和编辑界面）
    [ShareSDK showShareActionSheet:nil // 要显示菜单的视图, iPad版中此参数作为弹出菜单的参照视图，只有传这个才可以弹出我们的分享菜单，可以传分享的按钮对象或者自己创建小的view 对象，iPhone可以传nil不会影响
                             items:nil
                       shareParams:shareParams
               onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
                   switch (state) {
                       case SSDKResponseStateSuccess:
                       {
                           UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"
                                                                               message:nil
                                                                              delegate:nil
                                                                     cancelButtonTitle:@"确定"
                                                                     otherButtonTitles:nil];
                           [alertView show];
                           break;
                       }
                       case SSDKResponseStateFail:
                       {
                           UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"
                                                                           message:[NSString stringWithFormat:@"%@",error]
                                                                          delegate:nil
                                                                 cancelButtonTitle:@"OK"
                                                                 otherButtonTitles:nil, nil];
                           [alert show];
                           break;
                       }
                       default:
                           break;
                   }
               }
     ];
}
#pragma mark - wknavigationdelegate
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [MBProgressHUD showMessage:@""];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [MBProgressHUD hideHUD];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [MBProgressHUD hideHUD];
}
- (void)clearCache
{
    //清除cookies
    NSHTTPCookie *cookie;
    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (cookie in [storage cookies])
    {
        [storage deleteCookie:cookie];
    }
    //    清除webView的缓存
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
}
@end
